package org.example.test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class test8 {
    public static void main(String[] args) throws IOException {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入要扫描的路径(绝对路径):");
        String rootPath=scanner.next();
        File rootDir=new File(rootPath);
        if (!rootDir.isDirectory()){
            System.out.println("您输入的目录不存在或这不是目录，退出");
            return;
        }
        System.out.println("请输入要找的文件名");
        String token=scanner.next();
        List<File> result=new ArrayList<>();
        scanDir(rootDir, token, result);
        System.out.println("找到的文件有："+result);
        for (File file : result){
            System.out.println(file.getCanonicalPath()+"请问您是否要删除Y/N");
            String in=scanner.next();
            if (in.toLowerCase().equals("y")){
                file.delete();
            }
        }
    }
    public static void scanDir(File dir, String token, List<File> result){
        File[] files=dir.listFiles();
        if (files == null || files.length == 0) {
            return;
        }
        for (File file : files){
            if (file.isDirectory()){
                scanDir(file, token, result);
            }else{
                if (file.getName().contains(token)){
                    result.add(file.getAbsoluteFile());
                }
            }
        }
    }
}
